//
//  HWBookPassTimeViewController.m
//  嗨文
//
//  Created by 响当当 on 2019/2/25.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "HWBookPassTimeViewController.h"
#import "HWBookPassTimeCell.h"

@interface HWBookPassTimeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, copy)NSArray *arr;
@end

@implementation HWBookPassTimeViewController

- (NSArray *)arr
{
    if (!_arr)
    {
//        _arr = @[@{@"title":@"15书券",@"time":@"7天后过期"},
//                 @{@"title":@"100书券",@"time":@"10天后过期"},
//                 @{@"title":@"60书券",@"time":@"15天后过期"}];
        _arr = [[NSArray alloc]init];
    }
    return _arr;
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,NAVIBAR_HEIGHT,SCREEN_WIDTH,SCREEN_HEIGHT-NAVIBAR_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = 0;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"书券有效期"];
    [self setDefaultNav];
    self.view.backgroundColor = LCHexColor(0xF5F5F5);
    [self getInfor];
    
    [self.view addSubview:self.tableView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self buryPoint];
}

#pragma mark - delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60*kUIScaleBasePlus;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HWBookPassTimeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HWBookPassTimeCell"];
    if (!cell)
    {
        cell = [[HWBookPassTimeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HWBookPassTimeCell"];
        cell.selectionStyle = 0;
    }
    
    HWBookPassTimeModel *model = [[HWBookPassTimeModel alloc]initWithDic:self.arr[indexPath.row]];
    [cell getDate:model];
    
    return cell;
}

#pragma mark - 获取书券有效期信息数据
- (void)getInfor
{
    
    NSDictionary *prarmers = @{
                               @"access_token" : current_User.token                               };
    
    WeakSelf(self)
    [MyInfoManager setBookPassTime:prarmers sucess:^(id  _Nonnull acceptData) {
        NSArray *arr = [NSArray arrayWithArray:acceptData[@"list"]];
        weakSelf.arr = arr;
        [weakSelf.tableView reloadData];
    } fail:^(NSError * _Nullable error) {
        DISMISS_SVP(1.0f);
        [SVProgressHUD showErrorWithStatus:@"获取失败，请检查网络是否良好"];
        DISMISS_SVP(1.0f);
    }];
}

- (void)buryPoint
{
    NSString *lastStayTime = [kUserDefaults objectForKey:@"time"];
    NSString *nowStayTime = [NSString stringWithFormat:@"%ld",[[HWUtilties getNowTimeTimestamp3] integerValue] - lastStayTime.integerValue];
    
    [HWUtilties buryPointWithStayTime:nowStayTime
                            timestamp:[HWUtilties getNowTimeTimestamp3]
                             memberId:current_User.userID
                              fromUrl:nil
                                toUrl:@"/bookTokenExpiry"
                               fromId:nil
                                 toId:@"98467eee43cde8f4"];
}
@end
